package com.yiya.ms.action;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Company;
import com.yiya.bean.Employee;
import com.yiya.bean.Renew;
import com.yiya.bean.SerCompany;
import com.yiya.bean.SysUser;
import com.yiya.model.RenewCplex;
import com.yiya.model.RenewCplexModel;
import com.yiya.service.CompanyService;
import com.yiya.service.EmployeeService;
import com.yiya.service.RenewService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/renew")
public class RenewAction extends BaseAction{
	@SuppressWarnings("unused")
	private final static Logger log= Logger.getLogger(NewsAction.class);
	
	@Autowired
	private RenewService renewService;
	
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 维修公司端页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/ser_company_renew_list") 
	public ModelAndView list(HttpServletRequest request){
		Map<String,Object>  context = getRootMap();
		return forword("renew/ser_company_renew_list",context); 
	}
	
	/**
	 * 维修公司端数据
	 * @param model
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/ser_company_renew_dataList") 
	public void  datalist(RenewCplexModel model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		if(user.getSuperAdmin()!=1){
			Employee employee = employeeService.queryByUserId(user.getId());
			SerCompany serCompany = new SerCompany();
			serCompany.setId(employee.getSerCompanyId());
			model.setSerCompany(serCompany);
		}
		List<RenewCplex> list = renewService.queryByList(model);
		int count = renewService.queryByCount(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/save")
	public void save(Renew bean,HttpServletRequest request,HttpServletResponse response){
		SysUser user = SessionUtils.getUser(request);
		Date nowTime = new Date();
		if(bean.getId() == null){
			Employee employee = employeeService.queryByUserId(user.getId());
			if(employee!=null){
				bean.setSerCompanyId(employee.getSerCompanyId());
			}
			bean.setCreateBy(user.getId());
			bean.setCreateTime(nowTime);
			renewService.add(bean);
		}else{
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			renewService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		renewService.delete(id,user);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/renew_list") 
	public ModelAndView renewList(HttpServletRequest request){
		Map<String,Object>  context = getRootMap();
		return forword("renew/renew_list",context); 
	}
	
	@RequestMapping("/renew_dataList") 
	public void  renewDataList(RenewCplexModel model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		List<RenewCplex> list = renewService.queryByList(model);
		int count = renewService.queryByCount(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response){
		Map<String,Object>  context = getRootMap();
		Renew bean = renewService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/handleOrder")
	public void handleOrder(Renew bean,HttpServletRequest request,HttpServletResponse response){
		if(bean.getApplyStatus()==1){
			SysUser user = SessionUtils.getUser(request);
			Date nowTime = new Date();
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			renewService.Update(bean);
			bean = renewService.queryById(bean.getId());
			Company company = companyService.queryById(bean.getCompanyId());
			Calendar expireDate = Calendar.getInstance();
			expireDate.setTime(company.getExpiryDate());
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(nowTime);
			
			if(expireDate.after(calendar)){
				//如果过期日期大于等于当前时间，新的过期日期等于旧的过期日期加上续费时长
				expireDate.add(Calendar.MONTH, bean.getDuration());
				company.setExpiryDate(expireDate.getTime());
			}else{
				//如果过期日期小于当前时间，新的过期日期等于当前时间加续费时长
				calendar.add(Calendar.MONTH, bean.getDuration());
				company.setExpiryDate(calendar.getTime());
			}
			
			company.setState(1);
			company.setUpdateBy(user.getId());
			company.setUpdateTime(nowTime);
			companyService.Update(company);
		}
		sendSuccessMessage(response, "保存成功");
	}
}
